package com.xteam.web.controller;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.web.servlet.ModelAndView;

import com.tangkf.utils.CommonUtils;
import com.tangkf.utils.bean.BeanHelper;
import com.tangkf.utils.bean.ReqHelper;
import com.xteam.db.page.Page;
import com.xteam.db.page.PageInfo;
import com.xteam.purview.entities.SysLogInfo;
import com.xteam.purview.service.impl.SystemLogFace;
import com.xteam.utils.WebTools;
import com.xteam.web.Error;

public class SysLogController extends PurViewController implements IController {

	SystemLogFace	face	= SystemLogFace.getInstance();

	public SysLogController() {
	}

	public ModelAndView delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
		response.setContentType(JSON_TYPE);

		String[] ids = ReqHelper.getRequestStrings(request, "id[]");
		int r = face.delBatch(SysLogInfo.class, SysLogInfo.PROP_ID, ids);

		Error er = r > 0 ? Error.create(r + "", "删除类别出错！") : Error.create();
		WebTools.writerJsonToResponse(response, er);
		return null;
	}
	
	public ModelAndView delByDate(HttpServletRequest request, HttpServletResponse response) throws Exception {
		response.setContentType(JSON_TYPE);
		
		String time = ReqHelper.getRequestString(request, SysLogInfo.PROP_OPTTIME);
		int r = face.deleteByDate(time);
		Error er = r > 0 ? Error.create(r + "", "删除类别出错！") : Error.create();
		WebTools.writerJsonToResponse(response, er);
		return null;
	}

	/**
	 * 返回所有日志/分页
	 * @see cn.mystic.web.former.PurViewController#find(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	public ModelAndView find(HttpServletRequest request, HttpServletResponse response) throws Exception {
		response.setContentType(JSON_TYPE);
		
		int size = ReqHelper.getRequestInt(request, "size");
		int pageNo = ReqHelper.getRequestInt(request, "pageNo");

		Page page = new PageInfo(size, pageNo + 1);
		
		PrintWriter out = response.getWriter();
		String data = JSONObject.fromObject(face.query(SysLogInfo.class, page)).toString();
		out.println(data);
		return null;
	}

	public ModelAndView save(HttpServletRequest request, HttpServletResponse response) throws Exception {
		response.setContentType(JSON_TYPE);

		SysLogInfo fn = new SysLogInfo();
		BeanHelper.requestToBean(fn, request);
		int r	= 0;
		if (CommonUtils.isEmpty(fn.getId())) {
			r	= face.save(fn);
		} else {
			r	= face.update(fn);
		}
		Error er	= r>0?Error.create(r+"", "保存系统日志出错！"):Error.create();
		WebTools.writerJsonToResponse(response, er);
		return null;
	}

}
